<?php
class Post_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('post_photo_model');
        $this->load->model('photo_model');
    }
    
    public function insert($post, $upload_photos = NULL) {
        $this->db->insert('post', $post);
        $res = $this->db->affected_rows();
        if ($res > 0) {
            $post_id = $this->db->insert_id();
            
            if (isset($upload_photos)) {
                foreach($upload_photos as $photo) {
                    $photo_id = $this->photo_model->insert($photo);
                    if ($photo_id !== -1) {
                        $result = $this->post_photo_model->insert(array('post_id' => $post_id, 'photo_id' => $photo_id));
                        if ($result === TRUE) {
                            // TODO: handle success
                        } else {
                            // TODO: handle error
                        }
                    } else {
                        // TODO: handle error
                    }
                }
            }
            
            return $post_id;
        } else {
            return -1;
        }
    }
    
    // get post information, only
    private function _get($post_id) {
        $query = $this->db->get_where('post', array('post_id' => $post_id));
        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return NULL;
        }
    }
    
    // get post information, with its author information
    public function get($post_id) {
        $this->db->from('post');
        $this->db->join('user', 'user.user_id = post.user_id', 'left');
        $this->db->where(array('post_id' => $post_id));
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->row_array();
        } else {
            return NULL;
        }
    }
    
    public function get_all() {
        $this->db->from('post');
        $this->db->join('user', 'post.user_id = user.user_id');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }
    
    public function get_first_page($num) {
        $this->db->join('user', 'post.user_id = user.user_id');
        $this->db->select('post.post_id, post.post_title, post.user_id,
                post.post_time, post.click_num, post.repost_num,
                post.post_like, post.post_topic, post.post_gender,
                post.post_birth, post.post_constel, post.post_residence,
                post.display, 
                user.email, user.nickname, user.gender');
        $this->db->order_by('post.post_id', "desc");
        // 读取时就将不显示（被删除）的帖子过滤，而不是在前台过滤
        $this->db->where(array(
                'post.display =' => '1'
                ));
        $query = $this->db->get('post', $num, 0);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }
    
    public function count_all() {    //统计总贴数
        return $this->db->count_all('post');
    }
    

    /*
     * 带分页的帖子列表查询
     *
     */    
    public function get_all_with_last_id($num, $last_id) {
        $this->db->join('user', 'post.user_id = user.user_id');
        $this->db->select('post.post_id, post.post_title, post.user_id,  
                post.post_time, post.click_num, post.repost_num, 
                post.post_like, post.post_topic, post.post_gender, 
                post.post_birth, post.post_constel, post.post_residence, 
                post.display, 
                user.email, user.nickname, user.gender');
        $this->db->order_by('post.post_id', "desc");
        // 读取时就将不显示（被删除）的帖子过滤，而不是在前台过滤
        $this->db->where(array(
                'post_id <' => $last_id,
                'post.display =' => '1'
                ));
        $query = $this->db->get('post', $num, 0);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }
    
    /*
     * 获得十大帖子
     *
     */    
    public function get_top_ten() {
        $this->db->join('post', 'post_top_ten_index.post_id = post.post_id');
        $this->db->join('user', 'post.user_id = user.user_id');
        $this->db->select('post.post_id, post.post_title, post.user_id,  
                post.post_time, post.click_num, post.repost_num, 
                post.post_like, post.post_topic, post.post_gender, 
                post.post_birth, post.post_constel, post.post_residence, post.post_value, 
                user.email, user.nickname, user.gender, 
                post_top_ten_index.position, post_top_ten_index.global_photo_id, post_top_ten_index.photo_location_cache');
                
        // 2013.3.18 by CCY: 过滤“被删”的帖子进十大
        $this->db->where('post.display', true);
        
        $this->db->order_by('post_top_ten_index.position', "asc");
        $query = $this->db->get('post_top_ten_index');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        } else {
            return NULL;
        }
    }
    
    public function get_repost_num($post_id) {
        $this->db->select('repost_num');
        $query = $this->db->get_where('post', array('post_id' => $post_id));
        if ($query->num_rows() > 0) {
            return $query->row()->repost_num;
        } else {
            return NULL;
        }
    }
    
    public function repost($post_id, $level_no = NULL) {
        if (isset($level_no)) {
            $this->db->set(array('repost_num' => $level_no));
        } else {
            $this->db->set('repost_num', 'repost_num + 1', FALSE);
        }
        $this->db->where(array('post_id' => $post_id));
        $this->db->update('post');
        $res = $this->db->affected_rows();
        if ($res > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    public function click($post_id) {
        $this->db->set('click_num', 'click_num + 1', FALSE);
        $this->db->where(array('post_id' => $post_id));
        $this->db->update('post');
        $res = $this->db->affected_rows();
        if ($res > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    public function get_albums($user_id) {
        $this->db->select('post_id');
        $query = $this->db->get_where('post', array('user_id' => $user_id));
        if ($query->num_rows() > 0) {
            $albums = array();
            foreach ($query->result_array() as $row) {
                $album = $this->_get_album($row['post_id']);
                if ($album !== NULL) {
                    $albums[] = $album;
                }
            }
            return $albums;
        } else {
            return NULL;
        }
    }
    
    private function _get_album_cover($post_id) {
        return $this->post_photo_model->get_one($post_id);
    }
    
    private function _get_album($post_id) {
        $cover = $this->_get_album_cover($post_id);
        if ($cover === NULL) {
            return NULL;
        } else {
            $post = $this->_get($post_id);
            if ($post === NULL) {
                return NULL;
            } else {
                $post['cover'] = $cover;
                return $post;
            }
        }
    }
    
    public function delete($post_id) {
        $this->db->set('display', false);
        $this->db->where('post_id', $post_id);
        $this->db->update('post');
        if ($this->db->affected_rows() > 0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}
?>